package com.sunshine.binding;

import com.sunshine.session.SqlSession;

import java.lang.reflect.Proxy;

/**
 * @author Mokairui
 * @description
 * @since 2022/10/30
 */
public class MapperProxyFactory<T> {

    private final Class<T> mapperInterface;
    
    public MapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }
    
    public T newInstance(SqlSession sqlSession) {
        final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface);
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), 
                new Class[]{mapperInterface}, 
                mapperProxy);
    }
}
